package com.qing.library_manager.service.Impl.readersImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.library_manager.dao.readers.readersMapper;
import com.qing.library_manager.domain.dto.ReadersDTO;
import com.qing.library_manager.domain.entity.Readers;
import com.qing.library_manager.domain.query.ReadersQuery;
import com.qing.library_manager.service.manager.readers.readersService;
import com.qing.library_manager.common.util.ResponseResult;
import com.qing.library_manager.common.util.SnowFlakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class readerServiceImpl extends ServiceImpl<readersMapper, Readers> implements readersService {

    private final readersMapper readersMapper;
    private final SnowFlakeIdWorker snowFlakeIdWorker;

    public readerServiceImpl(com.qing.library_manager.dao.readers.readersMapper readersMapper,
                             SnowFlakeIdWorker snowFlakeIdWorker) {
        this.readersMapper = readersMapper;
        this.snowFlakeIdWorker = snowFlakeIdWorker;
    }

    /**
     * 添加读者
     *
     * @param readersDTO 书籍传输对象
     * @return
     */
    @Override
    public ResponseResult increaseReaders(ReadersDTO readersDTO) {
        int i = readersMapper.queryReadersNum(readersDTO.getReadersName());
        if (i > 0) {
            return ResponseResult.FAILED();
        }
        /**
         * 补全数据
         */
        Readers readers = new Readers();
        readers.setReadersId(String.valueOf(snowFlakeIdWorker.nextId()));
        readers.setReadersName(readersDTO.getReadersName().trim());
        readers.setReadersPhone(readersDTO.getReadersPhone().trim());
        readers.setReadersSex(readersDTO.getReadersSex().trim());
        readers.setReadersOccupation(readersDTO.getReadersOccupation().trim());
        readers.setReadersStatus("1");
        int flag = readersMapper.insert(readers);
        return flag > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }

    /**
     * 更新读者信息
     *
     * @param readersDTO 书籍传输对象
     * @return
     */
    @Override
    public ResponseResult updateReadersByName(ReadersDTO readersDTO) {
        int flag = readersMapper.updateReadersByName(readersDTO);
        return flag > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }

    /**
     * 删除读者信息
     *
     * @param readersShowId
     * @return
     */
    @Override
    public ResponseResult deleteReadersByName(int readersShowId) {
        int flag = readersMapper.deleteReadersByName(readersShowId);
        return flag > 0 ? ResponseResult.SUCCESS() : ResponseResult.FAILED();
    }

    /**
     * 查询所有读者
     *
     * @param
     * @return
     */
    @Override
    public ResponseResult queryAllReadersInit(int pageNum, int pageSize, ReadersQuery readersQuery) {
        Page<ReadersDTO> page = new Page<>(pageNum, pageSize);
        IPage<ReadersDTO> readersDTOIPage = readersMapper.queryAllReadersInit(page, readersQuery);
        return readersDTOIPage != null ? ResponseResult.SUCCESS(readersDTOIPage) : ResponseResult.FAILED();
    }

    /**
     * 根据id查询用户信息
     *
     * @param readersShowId
     * @return
     */
    @Override
    public ResponseResult queryReadersById(int readersShowId) {
        ReadersDTO readersDTO = readersMapper.queryReadersById(readersShowId);
        return readersDTO != null ? ResponseResult.SUCCESS(readersDTO) : ResponseResult.FAILED();
    }

    /**
     * 获取全部数据
     *
     * @return
     */
    @Override
    public ResponseResult queryAllReaders() {
        List<ReadersDTO> readersDTOS = readersMapper.queryAllReaders();
        return readersDTOS != null ? ResponseResult.SUCCESS(readersDTOS) : ResponseResult.FAILED();
    }
}
